//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:20-4-14
// @Time:下午2:33
//

#ifndef TESTSERVER_DATABASEHELPER_H
#define TESTSERVER_DATABASEHELPER_H

#include "SQLiteCpp/Database.h"
#include "UserDataBaseFactory.h"
#include "IoTDataBaseFactory.h"


#ifndef DATABASENAME
#define DATABASENAME "testGeeJoan.db"
#endif

enum DataBaseEnable
{
    ENABLE_USER_DB = 0x0001,
    ENABLE_IOT_DB = 0x0002,

};

class DataBaseHelper
{
public:
    DataBaseHelper(std::string filename_db3) : m_Db( filename_db3,
                                                     SQLite::OPEN_READWRITE |
                                                     SQLite::OPEN_CREATE,
                                                     2000 ) // Open a database file in r/w mode, and set timeout 2000ms
    {
        userDataBaseFactory = new UserDataBaseFactory( &m_Db );
        ioTDataBaseFactory = new IoTDataBaseFactory( &m_Db );
    }


    DataBaseHelper(std::string filename_db3, uint32_t enable) : m_Db( filename_db3,
                                                                      SQLite::OPEN_READWRITE |
                                                                      SQLite::OPEN_CREATE,
                                                                      2000 ) // Open a database file in r/w mode, and set timeout 2000ms
    {
        if ((enable & ENABLE_USER_DB) == ENABLE_USER_DB)
        {
            userDataBaseFactory = new UserDataBaseFactory( &m_Db );
        }

        if ((enable & ENABLE_IOT_DB) == ENABLE_IOT_DB)
        {
            ioTDataBaseFactory = new IoTDataBaseFactory( &m_Db );
        }
    }

    UserDataBaseFactory *getUserDataBaseFactory() const
    {
        return userDataBaseFactory;
    }

    IoTDataBaseFactory *getIoTDataBaseFactory() const;

    virtual ~DataBaseHelper();

    //检测表是否存在,如果不存在则h创建之
    int tableCheckExists();

private:
    SQLite::Database m_Db;

    UserDataBaseFactory *userDataBaseFactory = nullptr;
    IoTDataBaseFactory *ioTDataBaseFactory = nullptr;
};


#endif //TESTSERVER_DATABASEHELPER_H
